﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data.Services.Client;
using System.Linq;
using System.Windows.Controls;
using AyurvedaUI.AyurvedaService;

namespace AyurvedaUI
{
    /// <summary>
    /// Interaction logic for PlantAdd.xaml
    /// </summary>
    public partial class PlantAdd : UserControl
    {
        private readonly DataServiceContext context = new DataServiceContext(new Uri(ConfigurationSettings.AppSettings["AyurvedaServiceURI"]));
        private readonly AyurvedaEntities entity = new AyurvedaEntities(new Uri(ConfigurationSettings.AppSettings["AyurvedaServiceURI"]));
        private List<Languages> languageList;

        public PlantAdd()
        {
            InitializeComponent();
            CallServiceBackgroundWorker(this);
        }

        private static void CallServiceBackgroundWorker(PlantAdd win)
        {
            var bw = new BackgroundWorker();
            bw.DoWork += BackgroundWorker_DoWork;
            bw.RunWorkerCompleted += win.BackgroundWorker_RunWorkerCompleted;
            bw.RunWorkerAsync(win);
        }

        private static void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            var win = e.Argument as PlantAdd;
            if (win != null)
            {
                win.languageList = (from languages in win.entity.Languages select languages).ToList();
            }
        }

        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            cbLanguage.ItemsSource = languageList;
            cbLanguage.DisplayMemberPath = "Name";
        }
    }
}
